<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['labdata:employee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['labdata:employee:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['labdata:employee:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5" v-show="false">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['labdata:employee:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employeeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="employeeId" />
      <el-table-column label="工号" align="center" prop="employeeNo" />
      <el-table-column label="姓名" align="center" prop="employeeName" />
      <el-table-column label="任职起始日期" align="center" prop="hireStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hireStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任职状态" align="center" prop="hireStatusOpt" :formatter="hireStatusOptFormat" />
      <el-table-column label="任职批文号 " align="center" prop="approvalNo" />
      <el-table-column label="任职角色" align="center" prop="employeeRoleOpt" :formatter="employeeRoleOptFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['labdata:employee:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['labdata:employee:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实验室工作人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="orgId">
          <el-select v-model="form.orgId" placeholder="请选择主键">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="任职类型" prop="hireTypeOpt">
          <el-select v-model="form.hireTypeOpt" placeholder="请选择任职类型">
            <el-option
              v-for="dict in hireTypeOptOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="employeeNo">
          <el-input v-model="form.employeeNo" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="姓名" prop="employeeName">
          <el-input v-model="form.employeeName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="任职起始日期" prop="hireStartDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.hireStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择任职起始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任职状态" prop="hireStatusOpt">
          <el-select v-model="form.hireStatusOpt" placeholder="请选择任职状态">
            <el-option
              v-for="dict in hireStatusOptOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任职批文号 " prop="approvalNo">
          <el-input v-model="form.approvalNo" placeholder="请输入任职批文号 " />
        </el-form-item>
        <el-form-item label="任职角色" prop="employeeRoleOpt">
          <el-select v-model="form.employeeRoleList" multiple placeholder="请选择任职角色">
            <el-option
              v-for="dict in employeeRoleOptOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmployee, getEmployee, delEmployee, addEmployee, updateEmployee, exportEmployee } from "@/api/labdata/employee";

export default {
  name: "Employee",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验室工作人员表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 任职类型字典
      hireTypeOptOptions: [],
      // 任职状态字典
      hireStatusOptOptions: [],
      // 任职角色字典
      employeeRoleOptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hireTypeOpt: [
          { required: true, message: "任职类型不能为空", trigger: "change" }
        ],
        employeeNo: [
          { required: true, message: "工号不能为空", trigger: "blur" }
        ],
        employeeName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        hireStartDate: [
          { required: true, message: "任职起始日期不能为空", trigger: "blur" }
        ],
        hireStatusOpt: [
          { required: true, message: "任职状态不能为空", trigger: "change" }
        ],
        approvalNo: [
          { required: true, message: "任职批文号 不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("hireTypeOpt").then(response => {
      this.hireTypeOptOptions = response.data;
    });
    this.getDicts("hireStatusOpt").then(response => {
      this.hireStatusOptOptions = response.data;
    });
    this.getDicts("employeeRoleOpt").then(response => {
      this.employeeRoleOptOptions = response.data;
    });
  },
  methods: {
    /** 查询实验室工作人员列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then(response => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 任职类型字典翻译
    hireTypeOptFormat(row, column) {
      return this.selectDictLabel(this.hireTypeOptOptions, row.hireTypeOpt);
    },
    // 任职状态字典翻译
    hireStatusOptFormat(row, column) {
      return this.selectDictLabel(this.hireStatusOptOptions, row.hireStatusOpt);
    },
    // 任职角色字典翻译
    employeeRoleOptFormat(row, column) {
      return this.selectDictLabels(this.employeeRoleOptOptions, row.employeeRoleOpt);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        employeeId: null,
        orgId: null,
        hireTypeOpt: null,
        employeeNo: null,
        employeeName: null,
        hireStartDate: null,
        hireStatusOpt: null,
        approvalNo: null,
        employeeRoleOpt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.employeeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实验室工作人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const employeeId = row.employeeId || this.ids
      getEmployee(employeeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改实验室工作人员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.employeeId != null) {
            updateEmployee(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmployee(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const employeeIds = row.employeeId || this.ids;
      this.$confirm('是否确认删除实验室工作人员编号为"' + employeeIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEmployee(employeeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有实验室工作人员数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportEmployee(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
